from wxuser.error import UserNotExistentErr
from wxuser.models import Users


def get_current_user(request, lock=False):
    openid = request.META.get("HTTP_X_WX_OPENID", None)
    if openid is None:
        raise UserNotExistentErr
    _query_set = Users.objects.filter(openid=openid)
    if lock:
        _query_set = _query_set.select_for_update()
    user = _query_set.first()
    if user is None:
        raise UserNotExistentErr
    return user
